package com.hongyun.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.constants.DefaultCreateByConstant;
import com.hongyun.tms.common.dto.mwork.MWorkItemQueryDto;
import com.hongyun.tms.common.dto.mwork.MWorkItemSaveReqDto;
import com.hongyun.tms.common.dto.tug.SelecSearchDTO;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.MWorkItemVO;
import com.hongyun.tms.entity.MWorkItem;
import com.hongyun.tms.mapper.MWorkItemMapper;
import com.hongyun.tms.service.IMWorkItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 作业项目表 服务实现类
 * </p>
 *
 * @author liPeng
 * @since 2024-06-11
 */
@Service
public class MWorkItemServiceImpl extends ServiceImpl<MWorkItemMapper, MWorkItem> implements IMWorkItemService {

    @Override
    public PageResponseDto<MWorkItemVO> pageList(MWorkItemQueryDto query) {
        Page<MWorkItem> page = new Page<>(query.getStartPage(), query.getPageSize());
        baseMapper.selectPage(page, new LambdaQueryWrapper<MWorkItem>()
                .eq(StringUtils.isNotBlank(query.getWorkTypeCode()), MWorkItem::getWorkTypeCode, query.getWorkTypeCode())
                .like(StringUtils.isNotBlank(query.getWorkItem()), MWorkItem::getWorkItem, query.getWorkItem())
        );
        return (PageResponseDto<MWorkItemVO>) new PageResponseDto(page, MWorkItemVO.class);
    }

    @Override
    public List<Map<String, Object>> getWorkItemList(SelecSearchDTO selectCommonSearch) {
        return baseMapper.getWorkItemList(selectCommonSearch);
    }

    /**
     * 新增或更新作业项目
     *
     * @param dto 作业项目数据传输对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateWorkItem(MWorkItemSaveReqDto dto) {
        MWorkItem workItem = MapperingUtils.copyProperties(dto, MWorkItem.class)
        .setUpdateTime(LocalDateTime.now())
        .setUpdateBy(DefaultCreateByConstant.userId)
        .setUpdateByName(DefaultCreateByConstant.userId.toString());

        if (dto.getId() == null) {
            workItem.setCreateBy(DefaultCreateByConstant.userId);
            workItem.setCreateByName(DefaultCreateByConstant.userId.toString());
            workItem.setCreateTime(LocalDateTime.now());
            save(workItem);
        } else {
            updateById(workItem);
        }
    }

    @Override
    public List<MWorkItemVO> export(MWorkItemQueryDto query) {
        long currentPage = 1;
        List<MWorkItemVO> allRecords = Lists.newArrayList();

        while (true) {
            query.setStartPage(currentPage);
            PageResponseDto<MWorkItemVO> page = this.pageList(query);
            List<MWorkItemVO> records = page.getRecords();
            allRecords.addAll(records);

            if (records.size() < query.getPageSize()) {
                // 当前页的记录数小于 pageSize，说明已经是最后一页
                break;
            }
            currentPage++;
        }

        return allRecords;
    }
}
